#include "StdAfx.h"


#include "CAttribute.h"

#include "Attrs.h"

#include <QtGlobal>
#include "volumereader.h"

CAttribute::CAttribute(CMFT *parent)
:attrDat(),resAttr(NULL),nresAttr(NULL),parentMFT(parent)
{
}

CAttribute::~CAttribute(void)
{
}
UINT64 CAttribute::size()
{
	if (isResident)
		return resAttr->ContentSize;
	else return nresAttr->ContentSize;
}
//TODO : deal with non resident attribute
void CAttribute::read( UINT64 startByte, UINT64 len, char* buffer )
{
	Q_ASSERT(startByte+len<=this->size());
	if (isResident)
		memcpy(buffer,(char*)resAttr+resAttr->ContentOffset,len);
	else {
		Q_ASSERT_X(startByte % 512==0,"read nonresident attribute","must start read at 512 bytes");
		UINT64 diskOffset = this->parentMFT->getAttPos(this->nresAttr->Header.TypeID,this->nresAttr->Header.ID,startByte);
		Q_ASSERT_X(len<=parentMFT->parentVR->clusterSize(),"read nonresident attribute","read at most one cluster in one time");
		//fill the buffer
		parentMFT->parentVR->read(diskOffset,len,buffer);
	}
}



QString CAttribute::name() const
{
	char * pName = NULL;
	int lName = 0;
	if (isResident) {
		pName = (char*)resAttr+resAttr->Header.NameOffset;
		lName = resAttr->Header.NameLength;
	}else {
		pName = (char*)nresAttr + nresAttr->Header.NameOffset;
		lName = nresAttr->Header.NameLength;
	}
	return wArrayToQString((WCHAR*)pName,lName);
}

QList<Range> CAttribute::getClusterRange()
{
	return clusterRange;
}
